package com.lifang.imsoa.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Test;

public class MobileUtils {
	/**
	 * @author fangyouhui
	 * 检测房东联系方式  
	 *1、 纯数字：只能11为
	 *2、12为  只能包含一个"-"    
	 * @return
	 */
	public static boolean checkHostMobile(String hostMobile){
//        if(hostMobile.length()>14 || hit(hostMobile, "-")>1){
//        	return false;
//        }
		if(!checkMobile(hostMobile)){
			return false;
		}
		return true;
	}
	
	public static boolean checkHostMobile(String hostName,String hostMobile){
		if(StringUtils.isBlank(hostName) || StringUtils.isBlank(hostMobile)){
			return false;
		}
		String[] hostMobileArray = hostMobile.split(",");
		String[] hostNameArray = hostName.split(",");
		if(hostMobileArray.length == 0 || hostNameArray.length == 0){
			return false;
		}
		if(hostMobileArray.length != hostNameArray.length){
			return false;
		}
		for(int i =0;i<hostNameArray.length;i++){
			if(StringUtils.isBlank(hostNameArray[i])){
				return false;
			}
		}
		for(int i = 0;i<hostMobileArray.length;i++){
			if(StringUtils.isBlank(hostMobileArray[i])){
				return false;
			}
			if(!checkHostMobile(hostMobileArray[i])){
				return false;
			}
		}
		//联系人不能重复
		if(hostMobileArray.length>1){
			if(hostMobileArray[0].equals(hostMobileArray[1])){
				return false;
			}
		}
		if(hostMobileArray.length>2){
			if(hostMobileArray[0].equals(hostMobileArray[1]) || hostMobileArray[0].equals(hostMobileArray[2]) || hostMobileArray[1].equals(hostMobileArray[2])){
				return false;
			}
		}
		return true;
	}
	
	public static int hit(String a, String b) {
        if (a.length() < b.length()) {
            return 0;
        }
        char[] a_t = a.toCharArray();
        char[] b_t = b.toCharArray();
        int count = 0, temp = 0, j = 0;
 
        for (int i = 0; i < a_t.length; i++) {
            // 保证一个连续的字符串 b 跟 a中某段相匹配
            if (a_t[i] == b_t[j] && j < b_t.length) {
                temp++;
                j++;
                // 此时连续的字符串 b 跟 已跟 a 中某段相匹配
                if (temp == b_t.length) {
                    count++;
                    temp = 0;
                    j = 0;
                }
            }
            // 只要有一个字符不匹配，temp计数从来
            else {
                temp = 0;
                j = 0;
            }
        }
        return count;
    }
	public static void main(String[] args) {
//		System.out.println(checkHostMobile("15945631230-100"));
//		System.out.println("136516-85162".matches("/\\d{1-7}-\\d{1-9}/"));
//		System.out.println(hit("124-65-", "-"));
		//
		checkMobile("021-02111111");
	}
	
	public static boolean checkMobile(String str){
		Pattern pattern = Pattern.compile("^(\\d{11,14})|(\\d{3,5}-\\d{6,9})");
//		Pattern pattern = Pattern.compile("^(1\\d{10})|(\\d{3}-\\d{8})");
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
	
	@Test
	public void test(){
		Assert.assertEquals(checkHostMobile(",",","), false);
//		Assert.assertEquals(checkHostMobile("234,","021--124563,"), false);
		
	}
}
